/*
 * 文件名：BizPlanController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wangw
 * 修改时间：2020年3月16日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.bpa.web;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.dto.bpa.BizPlanDetailDto;
import top.infopub.dto.bpa.BizPlanListDto;
import top.infopub.dto.bpa.BpaPublicProInfoDto;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.bpa.dao.BizPlanDao;
import top.infopub.mgr.bpa.jsonVo.BizPlanDetailJsonVo;
import top.infopub.mgr.bpa.service.BizPlanService;
import top.infopub.mgr.bpa.service.BpaProcessManageService;
import top.infopub.mgr.bpa.service.ProContractService;
import top.infopub.mgr.bpa.vo.BizPlanVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.mgr.mkt.service.ProjectInfoService;
import top.infopub.model.bpa.BizPlan;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;


/**
 * @author wangw
 * @version 2020年3月16日
 * @see BizPlanController
 * @since
 */
@Controller
@RequestMapping("/bizPlan")
public class BizPlanController extends GenericFlowController {

    @Autowired
    private ProContractService proContractService;

    @Autowired
    private BizPlanService bizPlanService;

    @Autowired
    private StaffInfoService staffInfoService;

    @Autowired
    private BizPlanDao bizPlanDao;

    @Autowired
    private BpaProcessManageService bpaProcessManageService;

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    @Autowired
    private ProjectInfoService projectInfoService;


    /**
     * 跳转列表页
     *
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        return "bpa/lay-bizPlan-list";
    }

    /**
     * 商务策划列表数据
     *
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<BizPlanListDto> queryDataByCondition(BizPlanVo bizPlanVo, ParamPage paramPage) {
        Page<BizPlanListDto> page = new Page<BizPlanListDto>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());

        page = bizPlanService.selectPageByDomainWhere(page, bizPlanVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 商务策划-新增  0=保存，1=提交
     * @throws ParseException
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(String bodyStr, Integer submitType) {

        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();

        String str = StringEscapeUtils.unescapeJava(bodyStr);
        BizPlanDetailJsonVo detailJson = JSON.parseObject(str, BizPlanDetailJsonVo.class);
        //String codes = codeGeneratorConfigService.generatorCode("bpa_biz_plan");
        RspVo rspVo = bizPlanService.save(detailJson,submitType,vo);
        return rspVo;
    }

    @RequestMapping("/toDealOrDetail")
    public String toDealOrDetail(HttpServletRequest request, String id, String proCode) {
        request.setAttribute("id", id);
        BizPlan bizPlan = bizPlanDao.queryInfoById(id);
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("isAuditFlag", "1");
        request.setAttribute("auditTabFlag", "0");//登录人和审批人一致使用该字段是否显示审核信息
        request.setAttribute("finFlag", "0");//指定财务人员进入审批页面必须填写财务信息
        if (bizPlan != null) {
            if (org.apache.commons.lang3.StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), bizPlan.getProcessStatus())
                    || org.apache.commons.lang3.StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), bizPlan.getProcessStatus())) {
                request.setAttribute("isAuditFlag", "0");
            }
            if (org.apache.commons.lang3.StringUtils.isNotBlank(bizPlan.getProcessId()) && bizPlan.getProcessId().length() > 10) {
                request.setAttribute("processId", bizPlan.getProcessId());
                String auditTabFlag = bpaProcessManageService.checkShowAuditTab(bizPlan.getProcessId(), vo);
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), bizPlan.getProcessStatus())
                        && StringUtils.equals("0", auditTabFlag)) {
                    request.setAttribute("isAuditFlag", "0");
                }
                request.setAttribute("auditTabFlag", auditTabFlag);
            }
        }
        request.setAttribute("proCode", proCode);
        return "bpa/lay-BusinessPlanning-detail";
    }

    @RequestMapping("/queryDetail")
    @ResponseBody
    public RspVo queryDetail(String id) {
        BizPlanDetailJsonVo detailJson = bizPlanService.getDetail(id);
        return RspVo.getSuccessResponse(detailJson);
    }

    //根据procode获取项目的基本信息
    @RequestMapping("/queryProjDetail")
    @ResponseBody
    public RspVo queryProjDetail(String proCode) {
        BizPlanDetailJsonVo bizPlanJson = new BizPlanDetailJsonVo();
        //项目的基础信息
        BpaPublicProInfoDto  dto = projectInfoService.queryBpaProInfoByProCode(proCode);
        BizPlanDetailDto info = new BizPlanDetailDto();
        BeanUtils.copyProperties(dto, info);
        bizPlanJson.setInfo(info);
        return RspVo.getSuccessResponse(bizPlanJson);
    }
}
